package com.puyuba.puyuba.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.bean.FindPriListBean;
import com.puyuba.puyuba.bean.SimpleMap;
import com.puyuba.puyuba.network.UserApi;
import com.pyb.java.basemodule.BaseActivity;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.cache.bean.UserInfoBean;
import com.pyb.java.commonutil.ToastHelper;
import com.pyb.java.commonutil.utils.MapUtils;
import com.pyb.java.commonutil.utils.RegularUtils;
import com.pyb.java.commonutil.utils.StringUtil;
import com.pyb.java.network.RequestBodyUtil;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;
import com.pyb.java.network.scheduler.RxSchedulers;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * 用户资料页面
 */
public class UserEditActivity extends BaseActivity {
    private ImageView ivBack;
    private EditText etName;
    private TextView etEdu;
    private TextView tvEduRequi;
    private EditText etPhone;
    private EditText etWeixin;
    private EditText etQq;
    private TextView tvIndustry;
    private TextView tvLevel1;
    private TextView tvLevel2;
    private TextView tvLevel3;
    private TextView tvLevel4;
    private EditText etOther;
    private TextView tvAdd;
    private FindPriListBean.DataBean dataBean;
    private List<TextView> textViewList = new ArrayList<>();
    private String level = "";
    private String eduRequi;
    private String name;
    private String edurequi;
    private int size;
    private int category = 820201; //默认职业 选择学历之后变成学历
    private List<String> customlist = Arrays.asList("本科","专科");
    private List<String> customlist2 = Arrays.asList("IT","学生","教育行业","公务员/事业单位","房地产","自由职业","其他");
    private List<String> customlist3 = Arrays.asList("小学","初中","高中","中专","大专","本科","研究生");
    private UserInfoBean userInfoBean;
    private String edureq;

    @Override
    public int getLayoutId() {
        return R.layout.activity_useredit;
    }

    public static void startActivity(Context context, FindPriListBean.DataBean bean) {
        Intent intent = new Intent(context, UserEditActivity.class);
        intent.putExtra("bean", JSON.toJSONString(bean));
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ivBack = (ImageView) findViewById(R.id.iv_back);
        etName = (EditText) findViewById(R.id.et_name);
        etEdu = (TextView) findViewById(R.id.et_edu);
        tvEduRequi = (TextView) findViewById(R.id.tv_edu_requi);
        etPhone = (EditText) findViewById(R.id.et_phone);
        etWeixin = (EditText) findViewById(R.id.et_weixin);
        etQq = (EditText) findViewById(R.id.et_qq);
        tvIndustry = (TextView) findViewById(R.id.tv_industry);
        tvLevel1 = (TextView) findViewById(R.id.tv_level1);
        tvLevel2 = (TextView) findViewById(R.id.tv_level2);
        tvLevel3 = (TextView) findViewById(R.id.tv_level3);
        tvLevel4 = (TextView) findViewById(R.id.tv_level4);
        etOther = (EditText) findViewById(R.id.et_other);
        tvAdd = (TextView) findViewById(R.id.tv_add);

        initData();
        initListener();
    }

    private void initData() {
        String bean = getIntent().getStringExtra("bean");
        dataBean = JSON.parseObject(bean, FindPriListBean.DataBean.class);
        userInfoBean = CacheUserInfo.getInstance().getsUserInfo();
        Collections.addAll(textViewList,tvLevel1,tvLevel2,tvLevel3,tvLevel4);

        etName.setText(dataBean.getName());
        size = dataBean.getSize();
        etEdu.setText(MapUtils.getKeyByValue(SimpleMap.getInstance().getEduNeedMap(),dataBean.getSize()));
        eduRequi = MapUtils.getKeyByValue(SimpleMap.getInstance().getCareerNeedMap(), dataBean.getEdulevelid());
        tvEduRequi.setText(eduRequi);
        etPhone.setText(dataBean.getMobile());
        etWeixin.setText(dataBean.getWeixin());
        etQq.setText(dataBean.getQq());
        tvIndustry.setText(dataBean.getIndustry());
        etOther.setText(dataBean.getContactlog());

        if (dataBean.getLevel().equals("A")){
            changestatus(0);
            level = "A";
        }else if (dataBean.getLevel().equals("B")){
            changestatus(1);
            level = "B";
        }else if (dataBean.getLevel().equals("C")){
            changestatus(2);
            level = "C";
        }else if (dataBean.getLevel().equals("D")){
            changestatus(3);
            level = "D";
        }
    }

    private void initListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvLevel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changestatus(0);
                level = "A";
            }
        });
        tvLevel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changestatus(1);
                level = "B";
            }
        });
        tvLevel3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changestatus(2);
                level = "C";
            }
        });
        tvLevel4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changestatus(3);
                level = "D";
            }
        });
        tvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkCommitData();
            }
        });
        tvEduRequi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showEduPickerView();
            }
        });
        tvIndustry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showIndustryPickerView();
            }
        });
        etEdu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCurEduPickerView();
            }
        });
    }

    private void checkCommitData() {
        name = etName.getText().toString().trim();
        if(StringUtil.isEmpty(name)){
            ToastHelper.show("姓名不能为空");
            return;
        }
        if (StringUtil.isEmpty(level)){
            ToastHelper.show("请选择意向等级");
            return;
        }
        edurequi = tvEduRequi.getText().toString();
        if (StringUtil.isEmpty(edurequi)){
            ToastHelper.show("请选择学历需求");
            return;
        }

        addUser();
    }

    private void addUser() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userid", userInfoBean.getUser().getCrm_user_id());
        map.put("customer_id",dataBean.getId());
        map.put("name", name);
        map.put("level", level);
        map.put("mobile", etPhone.getText().toString().trim());
        map.put("category", category);
        edureq = tvEduRequi.getText().toString();
        if (edureq.equals("本科") || edureq.equals("专科")){
            map.put("edulevelid", SimpleMap.getInstance().getValue(edureq));
        }else {
            map.put("edulevelid", dataBean.getEdulevelid());
        }
        map.put("industry", SimpleMap.getInstance().getIndustryValue(tvIndustry.getText().toString()));
        map.put("size", size);
        map.put("weixin", etWeixin.getText().toString().trim());
        map.put("qq", etQq.getText().toString().trim());
        map.put("contactlog", etOther.getText().toString().trim());
        RequestBody bodyByMap = RequestBodyUtil.getRequestBodyByMap(map);
        showLoadingDialog();
        RetrofitManger.getInstance().createService(UserApi.class)
                .update(bodyByMap)
                .compose(RxSchedulers.<ResponseEntity<Object>>applySchedulers())
                .subscribe(new ResponseObserve<Object>(){

                    @Override
                    public void onSuccess(ResponseEntity<Object> responseEntity) {
                        closeLoadingDialog();
                        ToastHelper.show("编辑成功");
                        finish();
                    }
                    @Override
                    public void onFail(String errorMsg) {
                        closeLoadingDialog();
                        if (errorMsg != null&&!errorMsg.equals("")){
                            ToastHelper.show(errorMsg);
                        }else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@NonNull Disposable disposable) {
                        addDisposable(disposable);
                    }
                });
    }

    private void showEduPickerView() {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(UserEditActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
//               返回的分别是三个级别的选中位置
                tvEduRequi.setText(customlist.get(options1));
                category = 820204;
            }
        })
                .setTitleText("")
                .setSelectOptions(0)//设置选择第一个
                .setOutSideCancelable(false)//点击背的地方不消失
                .build();
        pvOptions.setPicker(customlist);
        pvOptions.show();

    }
    private void showIndustryPickerView() {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(UserEditActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                tvIndustry.setText(customlist2.get(options1));
            }
        })
                .setTitleText("")
                .setSelectOptions(0)//设置选择第一个
                .setOutSideCancelable(false)//点击背的地方不消失
                .build();
        pvOptions.setPicker(customlist2);
        pvOptions.show();

    }

    private void showCurEduPickerView() {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(UserEditActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
//               返回的分别是三个级别的选中位置
                etEdu.setText(customlist3.get(options1));
                size = options1+1;
            }
        })
                .setTitleText("")
                .setSelectOptions(0)//设置选择第一个
                .setOutSideCancelable(false)//点击背的地方不消失
                .build();
        pvOptions.setPicker(customlist3);
        pvOptions.show();
    }
    private void changestatus(int posi) {
        for (int i = 0; i < textViewList.size(); i++) {
            if (posi == i){
                textViewList.get(i).setTextColor(ContextCompat.getColor(UserEditActivity.this,R.color.white));
                textViewList.get(i).setBackgroundResource(R.drawable.bg_004d80_dp9);
            }else {
                textViewList.get(i).setTextColor(ContextCompat.getColor(UserEditActivity.this,R.color.color_9B));
                textViewList.get(i).setBackgroundResource(R.drawable.shape_input_bg);
            }
        }
    }
}
